package com.doyens.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.doyens.common.R;
import com.doyens.dto.CostDto;
import com.doyens.entity.Cost;
import com.doyens.entity.Student;
import com.doyens.service.CostService;
import com.doyens.service.StudentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Api(tags = "学生费用接口")
@RestController
@Slf4j
@RequestMapping("/cost")
public class CostController {
    @Autowired
    private CostService costService;

    @Autowired
    private StudentService studentService;


    //    分页查询，查询全部
//    模糊查
    @GetMapping("/page")
    @ApiOperation(value = "分页查询全部学生费用的方法")
    public R<Page> page(int page, int pageSize) {
//        分页构造器
        Page<Cost> pageInfo = new Page<>(page, pageSize);
        Page<CostDto> costDtoPage = new Page<>();
//        条件构造器
        LambdaQueryWrapper<Cost> lqw = new LambdaQueryWrapper<>();
//       添加排序条件,更新时间的排序
        lqw.orderByAsc(Cost::getUpdateTime);
        //执行分页查询
        costService.page(pageInfo, lqw);


//        对象拷贝
        BeanUtils.copyProperties(pageInfo, costDtoPage, "records");
        List<Cost> records = pageInfo.getRecords();

        List<CostDto> list = records.stream().map((item) -> {
            CostDto costDto = new CostDto();
            BeanUtils.copyProperties(item, costDto);
//            学生对象id
            Integer studentId = item.getStudentId();

//            根据id查询对象
            Student student = studentService.getById(studentId);
            log.info("查询到的学生名字:{}", student.getSname());

//            先判断，然后取出student里面名字，存到dto里，这样cost就可以显示出来了
            String sname = student.getSname();
            costDto.setSname(sname);

            return costDto;
        }).collect(Collectors.toList());
        costDtoPage.setRecords(list);
        return R.success(costDtoPage);
    }


    //    根据id删除
    @ApiOperation(value = "删除学生费用方法")
    @DeleteMapping
    public R<String> delete(Integer costId) {
        log.info("删除id：{}", costId);
        costService.removeById(costId);
        return R.success("删除学生费用成功");
    }


    //    添加
    @PostMapping
    @ApiOperation(value = "学生费用添加方法")
    public R<String> add(Cost cost) {
        log.info("添加学生费用：{}", cost.toString());
        costService.save(cost);
        return R.success("添加成功");
    }


    //    根据id查询费用信息，回显
    @GetMapping("{id}")
    @ApiOperation("查询单个费用，用于回显数据")
    public R<Cost> getById(@PathVariable Integer id) {
        log.info("根据id查询费用：{}", id);
        Cost cost = costService.getById(id);
        CostDto costDto = new CostDto();
        BeanUtils.copyProperties(cost,costDto);
        Integer studentId = cost.getStudentId();
        Student student = studentService.getById(studentId);
        if (student!=null){
            String sname = student.getSname();
            costDto.setSname(sname);
        }
        if (cost != null) {
            return R.success(costDto);
        }
        return R.error("没有查询到费用");
    }


    //    修改
    @PutMapping
    @ApiOperation("修改学生费用方法")
    public R<String> update(Cost cost) {
        log.info("修改费用信息：{}", cost);
        costService.updateById(cost);
        return R.success("修改成功");

    }
}








